$(function(){
    $(".modify-pwd").on("click",function(){
        var id=$(".modify-pwd").attr("data-id");
        ShowPswdBox();
        $(".qr").on("click",function(){
            var old_pwd=$(".old_pwd").val();
            var pwd=$(".pwd").val();
            var re_pwd=$(".re_pwd").val();
            if(old_pwd==""||pwd==""||re_pwd==""){
                layer.msg("请完善信息")
            }else if(pwd!=re_pwd){
                layer.msg("两次密码输入不一致")
            }else{
                ajax("post","/admin/modify-pwd",{id:id,old_pwd:old_pwd,pwd:pwd,re_pwd:re_pwd},function success(data){
                    if(data.code==200){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            location.href="/admin/login";
                        }, 1000);
                    }else{
                        layer.msg(data.msg);
                    }
                })
            }
        });
        $(".qx").on("click",function(){
            $(".filter").hide();
        })
    });
});

function ShowPswdBox(){
	var dIv=$("<div class='filter'><div class='pswd'><h3 class='m_p'>修改密码</h3><input type='password' class='old_pwd' placeholder='请输入旧密码'><input type='password' class='pwd' placeholder='请输入新密码'><input type='password' class='re_pwd' placeholder='请重新输入新密码'><div class='btn_box'><a class='btn qx'>取消</a><a class='btn btn-success qr'>确认</a></div></div></div>");
    $("body").append(dIv);
};

//修改店铺logo
var uploaderHeader = {
    'data':{
        'token':'',
        'uploadDomain':'',
        'domain':''
    },
    //  上传成功后的回调.
    'complete':function(res){
        // 这里只需要res.key
        var url="http://"+uploaderHeader.data.domain +"/"+ res.key;
        console.log(url);
        $('#shop_logo').attr('src',url);
        $.ajax({
            'type':'POST',
            'url':'/shop/modify-logo',
            'data':{
                logo:res.key
            },
            'dataType':'json',
            'success':function(data)
            {
                layer.closeAll();
                layer.alert(data.msg);
            }

        });
    },
    // 上传时的错误.
    'error':function () {
        console.dir(arguments);
    },
    // 分片上传时的进度展示功能在此书写.
    'next':function (response) {
        // var chunks = response.chunks || [];
        // var total = response.total;
    },
    // 初始化获取token.
    'init':function(){
        $.ajax({
            'url':'/upload/get-token',
            'type':'POST',
            'dataType':'json',
            'success':function(data){
                if(data.code != 200)
                {
                    layer.alert(data.msg);
                    return false;
                }
                uploaderHeader.data.token = data.data.token;      // token
                uploaderHeader.data.domain = data.data.domain;    // 显示域名
                // uploader.data.uploadDomain = 'idv093d.qiniudns.com';    // 上传域名
                // console.log(uploader.data.domain)
            }
        });
    },
    'eventBind':function(){
        this.init();
        $('#logo').on('change',function () {
            var token = uploaderHeader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };

            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };

            var file = this.files[0];

            var observable;

            if (file) {
                var files=$("#logo")[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploaderHeader.next,
                    error: uploaderHeader.error,
                    complete: uploaderHeader.complete
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
    }
};

$(document).ready(function(){
    uploaderHeader.eventBind();
});

